package com.example.oracle.dao;

import com.example.oracle.domain.User;
import org.apache.ibatis.jdbc.SQL;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
 * 用户SqlProvider类
 *
 * @author  nanjishiu
 * @version 1.0, 2017/11
 */
public class UserSqlProvider {

    private final static String TABLE_NAME = "users";
    private static final Logger logger = LogManager.getLogger(UserSqlProvider.class);

    public String insert(final User user) {
        String sql = new SQL() {
            {
                INSERT_INTO(TABLE_NAME);
                if (user.getUsername() != null) {
                    VALUES("username", "#{username}");
                }
                if (user.getPassword() != null) {
                    VALUES("password", "#{password}");
                }
                if (user.getName() != null) {
                    VALUES("name", "#{name}");
                }
                if (user.getEmail() != null) {
                    VALUES("email", "#{email}");
                }
                if (user.getPhone() != null) {
                    VALUES("phone" ,"#{phone}");
                }
                if (user.getRole() != null) {
                    VALUES("role", "#{role}");
                }else{
                    VALUES("role", "0");
                }
                if (user.getCreateTime() != null) {
                    VALUES("create_time", "#{createTime}");
                }

                if (user.getCreateIp() != null) {
                    VALUES("create_ip", "#{createIp}");
                }

                if (user.getLastTime() != null) {
                    VALUES("last_time", "#{lastTime}");
                }

                if (user.getLastIp() != null) {
                    VALUES("last_ip", "#{lastIp}");
                }

                if (user.getErrorTimes() != null) {
                    VALUES("error_times", "#{errorTimes}");
                }
                if (user.getSprotsmanId() != null) {
                    VALUES("sprotsman_id", "#{sprotsmanId}");
                }
                if (user.getLocationId() != null) {
                    VALUES("location_id", "#{locationId}");
                }
                if (user.getOrganizationId() != null) {
                    VALUES("organization_id", "#{organizationId}");
                }
                if (user.getStatus() != null) {
                    VALUES("status", "#{status}");
                }else{
                    VALUES("status", "0");
                }
            }
        }.toString();
        System.out.println(sql);
        return sql;
    }

    public String update(final User user) {
        String sql = new SQL() {
            {
                UPDATE(TABLE_NAME);
                WHERE("id = #{id}");
                if (user.getUsername() != null) {
                    SET("username = #{username}");
                }
                if (user.getPassword() != null) {
                    SET("password = #{password}");
                }
                if (user.getName() != null) {
                    SET("name = #{name}");
                }
                if (user.getEmail() != null) {
                    SET("email = #{email}");
                }
                if (user.getPhone() != null) {
                    SET("phone = #{phone}");
                }
                if (user.getRole() != null) {
                    SET("role = #{role}");
                }
                if (user.getCreateTime() != null) {
                    SET("create_time = #{createTime}");
                }

                if (user.getCreateIp() != null) {
                    SET("create_ip = #{createIp}");
                }

                if (user.getLastTime() != null) {
                    SET("last_time = #{lastTime}");
                }

                if (user.getLastIp() != null) {
                    SET("last_ip = #{lastIp}");
                }

                if (user.getErrorTimes() != null) {
                    SET("error_times = #{errorTimes}");
                }
                if (user.getSprotsmanId() != null) {
                    SET("sprotsman_id = #{sprotsmanId}");
                }
                if (user.getLocationId() != null) {
                    SET("location_id = #{locationId}");
                }
                if (user.getOrganizationId() != null) {
                    SET("organization_id = #{organizationId}");
                }
                if (user.getStatus() != null) {
                    SET("status = #{status}");
                }
            }
        }.toString();
        System.out.println(sql);
        return sql;
    }

    public String getList(Map<String, Object> params) {
        String sql = new SQL() {{
            SELECT("*");
            FROM(TABLE_NAME);
            if (params.get("username")!=null) {
                String username = (String) params.get("username");
                if (username!="") {
                    WHERE("username LIKE concat(concat('%',#{username}),'%')");
                }
            }
            if (params.get("phone")!=null) {
                String phone = (String) params.get("phone");
                if (phone!="") {
                    WHERE("phone = #{phone}");
                }
            }
            if (params.get("orderBy")!=null&&params.get("order")!=null){
                String orderBy = (String) params.get("orderBy");
                String order = (String) params.get("order");
                ORDER_BY(orderBy+" "+order);
            }
        }}.toString();
        if (params.get("pageSize")!=null&&params.get("page")!=null){
            int pageSize = (int) params.get("pageSize");
            int page = (int) params.get("page");
            sql = "SELECT * FROM (SELECT tt.*, ROWNUM AS rowno FROM (  "+sql +") tt WHERE ROWNUM < "+page * pageSize+
                    ") table_alias WHERE table_alias.rowno >= "+(page - 1) * pageSize;
        }
        System.out.println(sql);
        System.out.println(params.get("username"));
        return sql;
    }

}
